﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    [Serializable]
    public class FuzzyLinkOutput:ResultBase
    {
        #region props
        private int _SrcFieldCount1;
        /// <summary></summary>
        public int SrcFieldCount1
        {
            get
            {
                return this._SrcFieldCount1;
            }
            set
            {
                this._SrcFieldCount1 = value;
            }
        }

        private int _SrcFieldCount2;
        /// <summary></summary>
        public int SrcFieldCount2
        {
            get
            {
                return this._SrcFieldCount2;
            }
            set
            {
                this._SrcFieldCount2 = value;
            }
        }

        private int _LinkedFieldCount;
        /// <summary></summary>
        public int LinkedFieldCount
        {
            get
            {
                return this._LinkedFieldCount;
            }
            set
            {
                this._LinkedFieldCount = value;
            }
        }
        #endregion

        #region ctor
        public FuzzyLinkOutput():base(Guid.Empty,Guid.Empty)
        {
            this._SrcFieldCount1 = 0;
            this._SrcFieldCount2 = 0;
            this._LinkedFieldCount = 0;
        }

        public FuzzyLinkOutput(Guid componentID, Guid jobTicketID,
            int srcFieldCount1, int srcFieldCount2, int linkedCount) 
            : base(componentID, jobTicketID)
        {
            this._SrcFieldCount1 = srcFieldCount1;
            this._SrcFieldCount2 = srcFieldCount2;
            this._LinkedFieldCount = linkedCount;
        }
        #endregion

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FuzzyLinkOutput output = new FuzzyLinkOutput();
                // output.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.SrcFieldCount1 = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "SrcFieldCount1", "0"));
                output.SrcFieldCount2 = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "SrcFieldCount2", "0"));
                output.LinkedFieldCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "LinkedFieldCount", "0"));
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcFieldCount1", this.SrcFieldCount1.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcFieldCount2", this.SrcFieldCount2.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LinkedFieldCount", this.LinkedFieldCount.ToString());

            return xNode;
        }
    }
}
